// SuperAgent是轻量级更为优化的ajax API
import request from 'superagent'

const state = {
    login_email: '',
    login_token: '',
    login_name: '',
    temp_email: '',
    temp_token: '',
    temp_name: ''
}
// 过滤   Getter 接受 state 作为其第一个参数：
const getters = {
    currentUser: state =>{
        return {
            email: state.login_email,
            token: state.login_token,
            name: state.login_name
        }
    }
}
// mutations 存放函数，更改state中的状态的逻辑，参数为state对象 修改state需要提交mutation 只能在组件中methods store.commit修改 
// 可传入额外的参数，即 mutation 的 载荷（payload），每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)
const mutations = {
    // 判断传入的数据是否与注册时候的一样 
    updateData(state,payload){
        switch(payload.name){
            case 'email':
                state.temp_email = payload.value;
                break;
            case 'token':
                state.temp_token = payload.value;
                break;
            case 'name':
                state.temp_name = payload.name;
                break;
            default:
                console.log('Error:error with vuex store,you father.')
        }
    },
    setUser(state,payload){
        state.login_email = payload.email;
        state.login_token = payload.token;
        state.login_name = payload.name;
    },
    getLocalUser(state){
        state.login_email = localStorage.getItem('email');
        state.login_token = localStorage.getItem('token');
        state.login_name = localStorage.getItem('name');
    },
    // 移除本地存储的用户信息
    logout(state){
        localStorage.removeItem('email');
        localStorage.removeItem('token');
        localStorage.removeItem('name');
        state.login_email = '';
        state.login_token = '';
        state.login_name = '';
    }
}

const actions = {
    login({commit}, payload){
        return new Promise((resolve, reject) => {
            request.get('https://douban.herokuapp.com/user/' + payload.email)
            // 设置头部 带上token  Authorization: 'Bearer ' + token
            .set('Authorization','Bearer' + payload.token)
            .then(res =>{
                // 提交 setUser 
                commit({
                    type: 'setUser',
                    email: res.body.email,
                    token: res.body.token,
                    name: res.body.name
                })
                resolve(res)
            },err => {
                // 在一个promise链中 只要任何一个promise被reject promise链就被破坏了 之后的都不会再执行
                reject(err)
            })
        })
    },
    register({commit},payload){
        return new Promise((resolve,reject) => {
            request.post('https://douban.herokuapp.com/user/')
            .send({
                email: payload.email,
                pass: payload.pass,
                name: payload.name
            })
            .then(res => {
                 // 在本地存储用户的信息
                localStorage.setItem('token', res.body.token),
                localStorage.setItem('email', res.body.email),
                localStorage.setItem('name', res.body.name)
                commit({
                    type: 'setUser',
                    email: res.body.email,
                    token: res.body.token,
                    name: res.body.name
                })
                resolve(res)
            },err=> {
                reject(err)
            })
        })
    }   
}

export default{
    state,
    getters,
    mutations,
    actions
}